package cn.edu.cdtu.healthclient.activity;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import cn.edu.cdtu.healthclient.connect.ConnectForRegister;
import cn.edu.cdtu.healthclient.domain.RegisterJson;
import cn.edu.cdtu.healthclient.service.HealthService;
import cn.edu.cdtu.healthclient.service.HealthService.HealthBinder;
import cn.edu.cdtu.healthclient.task.ActivityTaskQueue;
import cn.edu.scu.healthclient.activity.R;

public class RegisterActivity extends Activity {
	/*
	 * 
	 */
	private ActivityTaskQueue taskQueue;
	public ActivityTaskQueue getTaskQueue(){
		return taskQueue;
	}
	/*
	 * 
	 */
	private HealthBinder binder;
	public HealthBinder getBinder(){
		return binder;
	}
	/*
	 * 
	 */
	private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			binder = null;
			taskQueue.shutdownTaskQueue();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (HealthBinder)service;
			if(!taskQueue.isRunning()){
				taskQueue.start();
			}
		}
	};
	/*
	 * 
	 */
	private final Intent serviceIntent = new Intent(HealthService.ACTION);
	private Button submitRegister;
	private EditText accountRegister,passwordRegister,userNameRegister;
	private String accountRegisterString,passwordRegisterString,userNameRegisterString;
	
	private ImageView closeTextButtonA,closeTextButtonB,closeTextButtonC;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		startService(serviceIntent);
    	taskQueue = new ActivityTaskQueue();
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_register);

		submitRegister=(Button)findViewById(R.id.SubmitRegister);
		accountRegister=(EditText)findViewById(R.id.AccountRegister);
		passwordRegister=(EditText)findViewById(R.id.PasswordRegister);
		userNameRegister=(EditText)findViewById(R.id.UserNameRegister);
		
		closeTextButtonA = (ImageView) findViewById(R.id.close_text_button_a_r);
		closeTextButtonB = (ImageView) findViewById(R.id.close_text_buttonmima_a_r);
		closeTextButtonC = (ImageView) findViewById(R.id.close_text_buttonid_a_r);
		
        closeTextButtonA.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(closeTextButtonA.isSelected()){
					accountRegister.setText("");
					closeTextButtonA.setSelected(false);
				}
			}
		});
        
       closeTextButtonB.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(closeTextButtonB.isSelected()){
					userNameRegister.setText("");
					closeTextButtonB.setSelected(false);
				}
			}
		});
       closeTextButtonC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(closeTextButtonC.isSelected()){
					passwordRegister.setText("");
					closeTextButtonC.setSelected(false);
				}
			}
		});
       
       accountRegister.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				closeTextButtonA.setSelected(true);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
       
       passwordRegister.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				closeTextButtonB.setSelected(true);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
       
       userNameRegister.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				closeTextButtonC.setSelected(true);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
       
       
		
		submitRegister.setOnClickListener
		(
			new OnClickListener()
			{
				public void onClick(View view)
				{
					final RegisterAsyncTask registerAsyncTask=new RegisterAsyncTask();
					accountRegisterString=accountRegister.getText().toString();
					passwordRegisterString=passwordRegister.getText().toString();
					userNameRegisterString=userNameRegister.getText().toString();
					if("".equals(accountRegisterString)||accountRegisterString==null)
					{
						Toast.makeText(getApplicationContext(),
								"用户账号不能为空",Toast.LENGTH_LONG).show();
					}
					else if("".equals(passwordRegisterString)||passwordRegisterString==null)
					{
						Toast.makeText(getApplicationContext(),
								"用户密码不能为空",Toast.LENGTH_LONG).show();
					}
					else if("".equals(userNameRegisterString)||userNameRegisterString==null)
					{
						Toast.makeText(getApplicationContext(),
								"用户姓名不能为空",Toast.LENGTH_LONG).show();
					}
					else
					{
						 taskQueue.addTask(new Thread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								registerAsyncTask.execute(
										 accountRegisterString,
										 passwordRegisterString,
										 passwordRegisterString);
							}
						}));
					}
				}
			}
		);
	}
	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		bindService(serviceIntent, 
        		serviceConnection, Service.BIND_AUTO_CREATE);
		super.onStart();
	}

	/* (non-Javadoc)
	 * <p>Title: onStop</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unbindService(serviceConnection);
	}
	/**
	 * 
	 * @ClassName: RegisterAsyncTask 
	 * @Description: TODO
	 * @author 曹碧霄
	 * @date 2015年3月22日 下午3:56:51 
	 *
	 */
	private class RegisterAsyncTask extends AsyncTask<String,Void,RegisterJson>
	{
		
		protected RegisterJson doInBackground(String ... params)
		{
			RegisterJson registerJson=null;
			try 
			{
				registerJson=ConnectForRegister.connect(
						accountRegisterString, 
						passwordRegisterString, 
						userNameRegisterString, 
						binder.getHttpClient());
			} 
			catch (ClientProtocolException e) 
			{
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			return registerJson;
		}
		
		protected void onPostExecute(RegisterJson registerJson)
		{
			Toast.makeText(getApplicationContext(),registerJson.getResult(), 
					Toast.LENGTH_LONG).show();
			Intent loginItent=new Intent(RegisterActivity.this,LoginActivity.class);
			RegisterActivity.this.finish();
			startActivity(loginItent);
		}
	}
}
